package com.ai.platform.system.service.impl;

import com.ai.platform.common.util.AiPlatformUtil;
import com.ai.platform.core.enums.LogType;
import com.ai.platform.core.enums.StateType;
import com.ai.platform.core.exception.BusinessException;
import com.ai.platform.core.log.LogManager;
import com.ai.platform.system.mapper.SysLoginLogMapper;
import com.ai.platform.model.dto.Input;
import com.ai.platform.model.dto.Output;
import com.ai.platform.system.entity.SysLoginLog;
import com.ai.platform.system.entity.SysUser;
import com.ai.platform.system.service.ISysLoginLogService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.TimerTask;

/**
 * <p>
 * 登录记录 服务实现类
 * </p>
 *
 * @author yeyingsheng
 * @since 2018-05-26
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements ISysLoginLogService {

    private static final Logger logger= LoggerFactory.getLogger(SysLoginLogServiceImpl.class);

    /**
     * 登录成功
     * @param input
     * @param output
     * @throws BusinessException
     */
    @Override
    public void loginLogSuccess(Input input, Output output) throws BusinessException {
        LogManager.getInstance().executeLog(new TimerTask() {
            @Override
            public void run() {
                try {
                    SysUser sysUser= (SysUser) AiPlatformUtil.getSessionObj(input,SysUser.class);
                    String msg=input.getValue("msg");
                    SysLoginLog loginLog = createLoginLog(LogType.LOGIN, sysUser.getId(), msg, StateType.SUCCESS, input.getIp());
                    SysLoginLogServiceImpl.super.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录日志异常!", e);
                }
            }
        });
    }

    /**
     * 登录失败
     * @param input
     * @param output
     * @throws BusinessException
     */
    @Override
    public void loginLogFailed(Input input, Output output) throws BusinessException {
        LogManager.getInstance().executeLog(new TimerTask() {
            @Override
            public void run() {
                String account=input.getValue("account");
                String msg=input.getValue("msg");
                SysLoginLog loginLog = createLoginLog(
                        LogType.LOGIN_FAIL, null, "账号[" + account + "],"+msg, StateType.FAIL, input.getIp());
                try {
                    SysLoginLogServiceImpl.super.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录失败异常!", e);
                }
            }
        });
    }

    /**
     * 创建登录日志
     */
    private SysLoginLog createLoginLog(LogType logType, Integer userId, String msg, StateType stateType, String ip) {
        SysLoginLog loginLog = new SysLoginLog();
        loginLog.setLogName(logType.getMessage());
        loginLog.setUserId(userId);
        loginLog.setCreateTime(new Date());
        loginLog.setState(stateType.getMessage());
        loginLog.setIp(ip);
        loginLog.setDesc(msg);
        return loginLog;
    }
}
